package com.usooft.metaTalent.core.system.service.base;

import com.usooft.framework.core.query.Limit;
import com.usooft.framework.core.query.Page;
import com.usooft.metaTalent.core.system.entity.Dict;
import com.usooft.metaTalent.core.system.mapper.DictMapper;
import com.usooft.metaTalent.core.system.query.DictQuery;
import com.usooft.metaTalent.core.system.service.DictService;
import com.usooft.metaTalent.core.system.wrapper.DictWrapper;
import java.lang.Long;
import java.lang.Override;
import java.lang.String;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.lang.NonNull;

public abstract class BaseDictService implements DictService {
    @Autowired
    private DictMapper dictMapper;

    @Override
    public Dict get(@NonNull Long id) {
        return find(id).orElseThrow();
    }

    @Override
    public Optional<Dict> find(@NonNull Long id) {
        return Optional.ofNullable(dictMapper.get(id));
    }

    @Override
    public List<DictWrapper> list(DictQuery query) {
        return dictMapper.list(query);
    }

    @Override
    public Page<DictWrapper> page(DictQuery query) {
        int rows = dictMapper.count(query.getFilter());
        return new Page<>(rows, rows == 0 ? new ArrayList<>() : dictMapper.list(query));
    }

    protected Optional<Dict> findOne(DictQuery.Filter filter, DictQuery.Sorts sorts) {
        List<DictWrapper> wrappers = list(DictQuery.builder()
                                .filter(filter)
                                .sorts(sorts)
                                .limit(Limit.rowCount(1))
                                .build());
        return wrappers.stream().findFirst().map(Function.identity());
    }

    protected Optional<Dict> findOne(DictQuery.Filter filter) {
        return findOne(filter, DictQuery.Sorts.builder().build());
    }

    @Override
    public int count(DictQuery.Filter filter) {
        return dictMapper.count(filter);
    }

    protected Dict internalAdd(String name, String description) {
        Dict dict = new Dict();
        dict.setName(name);
        dict.setDescription(description);

        dict.setCreatedDate(LocalDateTime.now());
        dict.setVersion(0);
        dictMapper.add(dict);
        return dict;
    }

    protected void internalUpdate(@NonNull Dict dict) {
        dict.setLastModifiedDate(LocalDateTime.now());

        int rows = dictMapper.update(dict);
        if (rows == 0) {
            throw new OptimisticLockingFailureException("更新失败。可能字典表 (" + dict.getId() + ") 的版本 (" + dict.getVersion() + ") 已不是最新");
        }
        dict.setVersion(dict.getVersion() + 1);
    }

    protected void internalDelete(@NonNull Dict dict) {
        int rows = dictMapper.delete(dict);
        if (rows == 0) {
            throw new OptimisticLockingFailureException("删除失败。可能字典表 (" + dict.getId() + ") 的版本 (" + dict.getVersion() + ") 已不是最新");
        }
    }
}
